// pages/myinfo/myinfo.js
import Toast from 'tdesign-miniprogram/toast/index';
import {
    getIntegral,
    getMyRegister,
    getMySign,
    resetPassword,
    getActivityList
} from '../../utils/request/myinfo/myinfoReq.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        empty: {
            show: false,
            image: 'http://ldjy.asc.jx.cn/assets/draw_fly.png'
        },
        isStudent: 3 <= wx.getStorageSync('roleId'),
        userInfo: {
            userNumber: wx.getStorageSync('userNumber'),
            name: wx.getStorageSync('name'),
            className: wx.getStorageSync('className'),
            roleId: wx.getStorageSync('roleId')
        },
        resetForm: {
            userNumber: wx.getStorageSync('userNumber'),
            password: '',
            tempPassword: ''
        },
        newPasswordError: false,
        registerActivityList: [],
        signActivityList: [],
        activityList: [],
        loginPopup: false,
        passwordPopup: false,
        inputStyle: 'border: 1px solid #6d6d6d; border-radius: 6px;'
    },
    onTabsChange(event) {
        const tab = event.detail.value;
        switch (tab) {
            case '1':
                this.getMyRegisterActivity();
                break;
            case '2':
                this.getMySignActivity();
                break;
            case '3':
                this.getMyPublishActivity();
                break;
            default:
                // 添加默认情况，以处理未匹配到的情况
                break;
        }
    },

    // 获取我报名的活动
    getMyRegisterActivity() {
        getMyRegister(wx.getStorageSync('userNumber')).then(res => {
            this.setData({
                registerActivityList: res.data
            })
        })
    },
    // 获取我签到的活动
    getMySignActivity() {
        getMySign(wx.getStorageSync('userNumber')).then(res => {
                this.setData({
                    signActivityList: res.data
                })
        })
    },

    // 获取我发布的活动
    getMyPublishActivity() {
        const form = {
            searchObject: {
                userNumber: wx.getStorageSync('userNumber')
            },
            pager: {
                currentPage: -1,
                pageSize: -1
            }
        }
        getActivityList(form).then(res => {
            this.setData({
                activityList: res.data
              })
        })
    },

    // 获取我申报的劳动
    getMyDeclareWork() {

    },
    // 获取我申报通过劳动
    getMyPassedWork() {

    },
      // 跳转活动详情页面
  gotoActivityDetail(row) {
    console.log( row.currentTarget.dataset.item.activityId)
    wx.setStorageSync('activityDetailId', row.currentTarget.dataset.item.activityId);

    wx.navigateTo({
      url: '/pages/activityDetail/activityDetail',
    })
  },
    // 调用微信登录
    wechatLogin(e) {
        console.log('正在调用微信登录')
    },
    // 跳转普通登录
    commonLogin(e) {
        console.log('正在使用账号登录')
        wx.navigateTo({
            url: '/pages/login/login',
        })
    },
    // 开启修改密码弹窗
    showResetPassword() {
        this.setData({
            passwordPopup: true,
        })
    },
    // 关闭修改密码弹窗
    onCloseResetPassword() {
        this.setData({
            passwordPopup: false
        })
    },
    // 关闭弹窗
    onClose() {
        this.setData({
            loginPopup: false,
        });
    },
    // 退出登录
    logout() {
        wx.setStorageSync('roleId', null)
        wx.setStorageSync('userNumber', null)
        wx.setStorageSync('name', null)
        wx.setStorageSync('className', null)
        wx.navigateTo({
            url: '../login/login',
        })
    },
    // 校验是否是登录状态
    checkLogin() {
        let userNumber = wx.getStorageSync('userNumber')
        if (userNumber !== undefined && userNumber !== '') {
            this.setData({
                loginPopup: false
            })
        } else {
            this.setData({
                loginPopup: true
            })
        }
    },
    // 获取用户信息
    getUserInfo() {
        getIntegral().then(res => {
            this.setData({
                userInfo: {
                    userNumber: wx.getStorageSync('userNumber'),
                    name: res.data.name,
                    className: res.data.className,
                    roleId: wx.getStorageSync('roleId'),
                    integral: res.data.integral
                }
            })
        })
    },

    // 新密码格式验证
    handleNewPasswordInput(event) {
        const value = event.detail.value
        if (/^[^\s]{8,16}$/.test(value)) {
            this.setData({
                'resetForm.tempPassword': value,
                newPasswordError: false
            })
        } else {
            this.setData({
                newPasswordError: true
            })
        }
    },
    handlePasswordInput(event) {
        const value = event.detail.value
        this.setData({
            'resetForm.password': value
        })
    },
    // 提交修改密码
    submitPassword() {
        resetPassword(this.data.resetForm).then(res => {

            if (res.data.code == 200) {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '修改成功',
                    icon: 'check-circle',
                    direction: 'column',
                });
                this.logout()
            } else {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: res.data.message,
                    icon: 'close',
                    direction: 'column',
                });
            }
        })
    },

    // 跳转加入我们
    gotoJoinGroup(){
        wx.navigateTo({
          url: './joinGroup/joinGroup',
        })
    },

    methods: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onShow() {
        this.getTabBar().init();
    },
    onLoad() {
        if (wx.getStorageSync('userNumber') === undefined) {
            this.setData({
                loginPopup: true
            })
        } else {
            this.getUserInfo()
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})